﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class MenuRefDAO//:BaseDAO<RELAXEntities,RelaxMenu>
   {
       #region value
       private RelaxDataContext _context = new RelaxDataContext();
       #endregion
       #region hàm lấy dữ liệu

       public IQueryable<RelaxMenuDataContract> GetAllMenu()
       {
           var query = from m in _context.MENU_REFs
                       join v in _context.MENU_VALUEs on m.id equals v.menuid 
                       select new RelaxMenuDataContract
                       {
                           Id = Convert.ToInt32(m.id),
                           Active = Convert.ToInt32(m.active??0),
                           Decription = v.decription,
                           Keyword = v.keyword,
                           Link = m.link,
                           Name = v.name,
                           Tag = v.tag,
                           Ord = Convert.ToInt32(m.ord??0),
                           Parent = m.parent,
                           CategoryId = Convert.ToInt32(m.categoryid ?? 0),
                           Target = m.target,
                           Type = Convert.ToInt32(m.type??0),
                           Languagecode = Convert.ToInt32(v.languagecode)
                       };
           return query;
       }
       public IQueryable<RelaxMenuDataContract> GetMenuId()
       {
           var query = from m in _context.MENU_REFs
                       select new RelaxMenuDataContract
                       {
                           Id = Convert.ToInt32(m.id),
                           Active = Convert.ToInt32(m.active),
                           Ord = Convert.ToInt32(m.ord),
                           Parent = m.parent,
                           CategoryId = Convert.ToInt32(m.categoryid??0),
                           Target = m.target,
                           Type = Convert.ToInt32(m.type)
                       };

           return query;
       }
       #endregion

       #region Add
       public void Add(MENU_REF menu)
       {
           _context.MENU_REFs.InsertOnSubmit(menu);
           _context.SubmitChanges();

       }
       #endregion

       #region hàm Update

       public new void Update(MENU_REF menuRef)
       {
           var obj = _context.MENU_REFs.Where(m => m.id == menuRef.id);
           if (obj.Any())
           {
               var firstOrdefault = obj.FirstOrDefault();
               if (firstOrdefault != null)
               {
                   firstOrdefault.active = menuRef.active;
                   firstOrdefault.link = menuRef.link;
                   firstOrdefault.target = menuRef.target;
                   firstOrdefault.categoryid = menuRef.categoryid;
                   firstOrdefault.ord = menuRef.ord;
                   firstOrdefault.parent = menuRef.parent;
                   firstOrdefault.type = menuRef.type;
                   _context.SubmitChanges();
               }
           }
       }
     
       #endregion

       #region hàm Delete

       public void Delete(long iId)
       {
           var obj = _context.MENU_REFs.Where(m => m.id == iId);
           if (obj.Any())
           {
               // ReSharper disable once AssignNullToNotNullAttribute
               _context.MENU_REFs.DeleteOnSubmit(obj.FirstOrDefault());
               _context.SubmitChanges();
           }
       }
       #endregion

   }
}
